package com.epfresh.api.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.aigestudio.wheelpicker.core.AbstractWheelPicker;
import com.aigestudio.wheelpicker.core.IWheelPicker;
import com.aigestudio.wheelpicker.widget.curved.WheelDatePicker;
import com.epfresh.supplier.R;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimePickerDialogbak extends Dialog implements View.OnClickListener {
    private View root;
    private ViewGroup container;
    private Button rButton,lButton;
    private IWheelPicker picker;
    private int index;
    private String data;
    private WheelDatePicker wheelDatePicker;
    private Context mContext;
    public TimePickerDialogbak(Context context) {
        super(context);
        mContext = context;
        padding = mContext.getResources().getDimensionPixelSize(R.dimen.WheelPadding);
        textSize = mContext.getResources().getDimensionPixelSize(R.dimen.TextSizeLarge);
        itemSpace = mContext.getResources().getDimensionPixelSize(R.dimen.ItemSpaceLarge);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(true);

        root = getLayoutInflater().inflate(R.layout.dialog_new_layout, null);
        container = (ViewGroup) root.findViewById(R.id.content);
        container.removeAllViews();
        rButton = (Button) root.findViewById(R.id.positiveButton);
        rButton.setOnClickListener(this);
        lButton = (Button) root.findViewById(R.id.negativeButton);
        lButton.setOnClickListener(this);

        wheelDatePicker = new WheelDatePicker(mContext);
//        wheelDatePicker.setPadding(padding, 0, padding, 0);
        wheelDatePicker.setBackgroundColor(0xFFeeeeee);
        wheelDatePicker.setTextColor(0xFF777777);
        wheelDatePicker.setCurrentTextColor(0xFF333333);
        wheelDatePicker.setLabelColor(0xFF333333);
        wheelDatePicker.setTextSize(textSize);
        wheelDatePicker.setItemSpace(itemSpace);

        setContentView(wheelDatePicker);


    }


    public void setCurrentDate(String time){
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            if (time == null ||"".equals(time)){

            }else {
                date = fmt.parse(time);
                Calendar calendar = Calendar.getInstance();
                if (date == null)
                    calendar.setTimeInMillis(System.currentTimeMillis());
                else
                    calendar.setTime(date);
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH) + 1;
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                wheelDatePicker.setCurrentDate(year, month, day);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void setContentView(View view) {
        if (view instanceof IWheelPicker) {
            picker = (IWheelPicker) view;
            picker.setOnWheelChangeListener(new AbstractWheelPicker.SimpleWheelChangeListener() {
                @Override
                public void onWheelScrollStateChanged(int state) {
//                    if (state != AbstractWheelPicker.SCROLL_STATE_IDLE) {
//                        rButton.setEnabled(false);
//                        lButton.setEnabled(false);
//                    } else {
//                        rButton.setEnabled(true);
//                        lButton.setEnabled(false);
//                    }
                }

                @Override
                public void onWheelSelected(int index, String data) {
                    TimePickerDialogbak.this.data = data;
                    TimePickerDialogbak.this.index = index;

                }
            });
        }
        container.addView(view);
        super.setContentView(root);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.positiveButton:
                dismiss();
                if (onPositiveClickListener != null){
                    onPositiveClickListener.onPositiveClickListener(index,data);
                }
                break;
            case R.id.negativeButton:
                dismiss();
                break;
        }
    }


    public void setTitle(String title){
        ((TextView)root.findViewById(R.id.title)).setText(title);
    }
    public void setLeftText(String text){
        ((TextView)root.findViewById(R.id.negativeButton)).setText(text);
    }
    public void setRightText(String text){
        ((TextView)root.findViewById(R.id.positiveButton)).setText(text);
    }
    private int padding;
    private int textSize;
    private int itemSpace;
    private OnPositiveClickListener onPositiveClickListener;
    public OnPositiveClickListener getOnPositiveClickListener() {
        return onPositiveClickListener;
    }

    public void setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        this.onPositiveClickListener = onPositiveClickListener;
    }
    public interface OnPositiveClickListener{
        public void onPositiveClickListener(int index1, String data);
    }
}